package com.xdw.uploaddemo.controller;
import com.xdw.uploaddemo.constant.ApiConstant;
import com.xdw.uploaddemo.model.ApiResult;
import com.xdw.uploaddemo.model.UploadFile;
import com.xdw.uploaddemo.service.UploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by issuser on 2019/12/12.
 */
@Controller
public class FileUploadController {
    @Value("${fileUploadPathWindow}")
    private String fileUploadPathWindow;
    @Value("${fileUploadPathLinux}")
    private String fileUploadPathLinux;
    @Value("${uploadFileUrlPrefix}")
    private String uploadFileUrlPrefix;
    @Autowired
    private UploadFileService uploadFileService;

    @RequestMapping("/ajax_upload")
    @ResponseBody
    public ApiResult<UploadFile> uploadOne(String fileOrginName, @RequestParam("file") MultipartFile file) {
        ApiResult<UploadFile> apiResult = new ApiResult<>();
        try {
            UploadFile uploadFile = uploadFileService.uploadOne("user1/",file);
            uploadFile.setPath(uploadFileUrlPrefix+"user1/"+uploadFile.getFilename());
            apiResult.setCode(ApiConstant.OK);
            apiResult.setData(uploadFile);
        } catch (IOException e) {
            e.printStackTrace();
            apiResult.setCode(ApiConstant.FILE_FAIL);
            apiResult.setMsg(ApiConstant.FILE_FAIL_MSG);
        }
        return apiResult;

    }


}
